Let us look at a simple example, where we have a box which is shared.
When any client clicks on it the box rotates in all the clients.
The sequence of events is as follows :
- A click is received in one of the clients and routed to the client's
local "touched" method.
- If the client is master, then it makes a remote procedure call to other
clients. This is done by calling sendApplSpecificMsgWithDist with the
distribution mode as allClientsExceptMe. The methodName parameter in this
call is rpc_rotateBox. This is the name of the function which will be called
locally at each client in response to receiving the rpc call from the master.
What it does (on the recipient's side) is to locally rotate the box. Now the
master calls actualRotateBox for its own rotation.
- If the client was not the master then it calls the rpc_touched method on
the master only using the "responderOnly" distribution flag. In response to
this the master then executes the rpc_touched method. Remember that the client
machine has not executed the local rotate box command yet. In the rpc_touched
method the master now sends rpc's to all clients to invoke their respective
local box commands. This is very similar to the part in the original
touched method. In fact,it duplicates it! This mechanism ensures that no
matter who clicked the object, the master will execute this piece of code.
This code acts as a "traffic cop" û the master takes control and sends
messages to all of the clients to rotate the box.
Note that if the client computer got the click and executed the master's
rpc_touched command, the master sends back a rotateBox message to the
original client as well. So we have a "ping-pong" effect which finally
results in everyone changing the color.
Be careful of circular loops û in this scheme it is all too easy to
get in a circular loop where a client calls a master procedure which
calls the same client procedure, over and over again.
This example consists of:
- A discussion of the source code,
- An active example of this world (coming soon!), and
- Exercises for you to try on your own.